<template>
	<view class="checkbox-panel-group">
		<view class="header">
			<view class="title">
				{{ title }}
			</view>
			<view class="toolber">
				<view :class="['checkbox', { selected: isSelectedAll }]" @click="clickAll">
					<view class="checkbox_icon">
						<u-icon name="checkbox-mark" color="#fff" size="28"></u-icon>
					</view>
					<view class="checkbox_label">
						{{ isSelectedAll ? '反选' : '全选' }}
					</view>
				</view>
			</view>
		</view>
		<scroll-view :scroll-into-view="scrollInto" scroll-with-animation="true" scroll-y="true" class="body"
			:style="{height: bodyHeight}">
			<template v-if="value.length !== 0">
				<view class="checkbox-panel" :id="`item-${index}`" v-for="(item, index) in value" :key="index">
					<view class="left">
						<view :class="['checkbox', { selected: list[index].selected }]" style="margin-top: 3rem;">
							<view class="checkbox_icon" @click="clickItem(item, index)">
								<u-icon name="checkbox-mark" color="#fff" size="28"></u-icon>
							</view>
						</view>
					</view>
					<slot :list="list" :index="index" :item="item" />
				</view>
			</template>
			<view v-else class="empty">
				<!-- <Empty /> -->
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import Empty from '@/components/empty/index.vue';

	export default {
		name: 'CheckboxPanelGroup',
		components: {
			Empty
		},
		props: {
			title: {
				type: String,
				default: '默认标题'
			},
			value: {
				type: Array,
				default: () => []
			},
			bodyHeight: {
				type: String,
				default: 'auto'
			},
		},
		data() {
			return {
				isSelectedAll: false,
				list: [],
				scrollInto:'item-999'
			}
		},
		watch: {
			value: {
				handler() {
					this.list = this.value.map((item, index) => {
						let selected = false;
						if (this.list.length !== 0) {
							if (this.list[index].selected) {
								selected = true;
							} else if (item.selected) {
								selected = true;
							}
						}
						return {
							item: item,
							selected,
						}
					});
				},
				deep: true
			},
		},
		methods: {
			clickAll() {
				this.isSelectedAll = !this.isSelectedAll;
				this.list.forEach((item) => {
					item.selected = this.isSelectedAll;
				});
			},
			clickItem(item, index) {
				this.list[index].selected = !this.list[index].selected;
				this.isSelectedAll = this.list.filter((item) => item.selected).length === this.list.length;
			},
			getSelections() {
				return this.list.filter((item) => item.selected).map((item) => item.item);
			},
			changeRightTitle(index) {
				// 测试
				this.scrollInto=`item-999`
				setTimeout(()=>{
					this.scrollInto = `item-${index}` //改变右侧id，使其滑动
				},500)
			}
		},
		beforeMount() {
			this.list = this.value.map((item) => {
				return {
					item: item,
					selected: false
				}
			});
		},
		mounted() {
			// setInterval(()=>{
			// 	this.changeRightTitle(1)
			// },1000)
		}
	}
</script>

<style scoped lang="scss">
	.checkbox-panel-group {
		font-size: 2rem;

		.header {
			display: flex;
			justify-content: space-between;
			align-items: center;

			.title {
				height: 8rem;
				display: flex;
				align-items: center;
				font-size: 2rem;
				font-weight: bold;
			}
		}

		.body {
			overflow-y: scroll;
		}
	}

	.checkbox {
		display: flex;
		align-items: center;

		.checkbox_icon {
			border-radius: 10rem;
			border: 1px solid #ccc;
			width: 4rem;
			height: 4rem;
			display: flex;
			justify-content: center;
			align-items: center;
			transition: all .3s ease;

			.u-icon__icon {
				background: #fff;
			}
		}

		.checkbox_label {
			margin-left: 1rem;
		}

		&.selected {
			.checkbox_icon {
				background-color: #40a7d9;
				width: 4rem;
				height: 4rem;

				.u-icon__icon {
					background: #fff;
				}
			}
		}
	}

	.checkbox-panel {
		display: flex;
		border: 1px solid #ccc;
		border-radius: 2rem;
		padding: 0 3rem 2rem;
		margin-top: 3rem;
		// min-height: 55rem;

		&:first-child {
			margin-top: 0;
		}
	}

	.empty {
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
	}
</style>